package com.kasinf.sample.mybatis.controller;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.kasinf.framework.common.pojo.CommonResult;
import com.kasinf.framework.common.pojo.PageParam;
import com.kasinf.framework.common.pojo.PageResult;
import com.kasinf.framework.common.util.object.BeanUtils;
import com.kasinf.sample.mybatis.controller.vo.UserVO;
import com.kasinf.sample.mybatis.dal.dataobject.UserDO;
import com.kasinf.sample.mybatis.service.UserService;
import lombok.RequiredArgsConstructor;
import org.dromara.trans.service.impl.TransService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author : Kaiser
 * @Date: 2025/5/19  11:05
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("user")
public class UserController {

    private final UserService userService;
    private final TransService transService;

    @GetMapping
    public CommonResult<PageResult<UserVO>> queryList(PageParam pageParam) {
        PageResult<UserDO> result = userService.selectPage(pageParam,
                new MPJLambdaWrapper<UserDO>().eq(UserDO::getStatus, 1));
        return CommonResult.success(BeanUtils.toPageBean(result, UserVO.class));
    }

    @PostMapping
    public ResponseEntity<UserDO> insert(@RequestBody UserDO userDO) {
        userService.save(userDO);
        return ResponseEntity.ok(userDO);
    }

    @PutMapping
    public ResponseEntity<UserDO> update(@RequestBody UserDO userDO) {
        userService.update(userDO);
        return ResponseEntity.ok(userDO);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        userService.delete(id);
        return ResponseEntity.noContent().build();
    }
}
